//CSE 430 - Operating Systems
//Partha Dasgupta
//Spring '12
//Project 1

//Alan Tu
//Kelly Jesionowski

#include "q.h"

qElem_t *queue1, *queue2;

void printQ(qElem_t** head){
	if(*head == NULL){
		printf("Queue is empty.\n");
	} else {
		qElem_t* iter;
		
		iter = *head;
		int i;
		int n = 0;
		do{
			if(iter->next == *head){
				printf("[%d]: %d", n, iter->data);
			} else {
				printf("[%d]: %d > ", n, iter->data);
			}
			
			iter = iter->next;
			n++;
		}while(iter != *head);
		printf("\n");
	}
}

int main(){
	qElem_t* first = NewItem();
	qElem_t* second = NewItem();
	qElem_t* third = NewItem();
	qElem_t* fourth = NewItem();
	
	first->data = 1;
	second->data = 2;
	third->data = 3;
	fourth->data = 4;
	
	qElem_t *deleted1, *deleted2;
	
	printf("--------------------------\n");
	printf("- Initializing one queue -\n");
	printf("--------------------------\n");
	InitQueue(&queue1);
	
	printf("\nPrinting empty queue: ");
	printQ(&queue1);	
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Queue: ");
	printQ(&queue1);
    
    printf("\nRotating queue.\n");
	RotateQ(&queue1);
    
    printf("\nAdding item = 1\n");
    AddQueue(&queue1, first);
    printf("Queue: ");
    printQ(&queue1);
    
    printf("\nAdding item = 2\n");
	AddQueue(&queue1, second);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nAdding item = 3\n");
	AddQueue(&queue1, third);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nRotating queue.\n");
	RotateQ(&queue1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nAdding deleted item.\n");
	AddQueue(&queue1, deleted1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nRotating queue.\n");
	RotateQ(&queue1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nRotating queue.\n");
	RotateQ(&queue1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nRotating queue.\n");
	RotateQ(&queue1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nRotating queue.\n");
	RotateQ(&queue1);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nAdding item = 4\n");
	AddQueue(&queue1, fourth);
	printf("Queue: ");
	printQ(&queue1);
    	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nAdding item = 2\n");
	AddQueue(&queue1, second);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\nDeleting head.\n");
	deleted1 = DelQueue(&queue1);
	printf("Deleted item is: %d\n", deleted1->data);
	printf("Queue: ");
	printQ(&queue1);
	
	printf("\n\n");
	printf("---------------------------\n");
	printf("- Initializing two queues -\n");
	printf("---------------------------\n");
	InitQueue(&queue1);
	InitQueue(&queue2);
	
	printf("\nPrinting previous queue (queue1): ");
	printQ(&queue1);
	printf("\nPrinting new queue (queue2): ");
	printQ(&queue2);
		
	printf("\nDeleting head of queue1.\n");
	deleted1 = DelQueue(&queue1);
	printf("Queue1: ");
	printQ(&queue1);
	printf("\nDeleting head of queue2.\n");
	deleted2 = DelQueue(&queue2);
	printf("Queue2: ");
	printQ(&queue1);
    
    printf("\nAdding item = 1 to queue 1\n");
    AddQueue(&queue1, first);
    printf("Queue1: ");
    printQ(&queue1);
    
    printf("\nAdding item = 2 to queue 2\n");
	AddQueue(&queue2, second);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nAdding item = 3 to queue 1\n");
	AddQueue(&queue1, third);
	printf("Queue1: ");
	printQ(&queue1);
	
	printf("\nAdding item = 4 to queue 2\n");
	AddQueue(&queue2, fourth);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nRotating queues.\n");
	RotateQ(&queue1);
	RotateQ(&queue2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nDeleting head of each queue.\n");
	deleted1 = DelQueue(&queue1);
	deleted2 = DelQueue(&queue2);
	printf("Queue1 deleted item is: %d\n", deleted1->data);
	printf("Queue2 deleted item is: %d\n", deleted2->data);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nAdding queue2 deleted item to queue1.\n");
	AddQueue(&queue1, deleted2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("\nAdding queue1 deleted item to queue2.\n");
	AddQueue(&queue2, deleted1);
	printf("Queue2: ");
	printQ(&queue2);
		
	printf("\nRotating queues.\n");
	RotateQ(&queue1);
	RotateQ(&queue2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nRotating queues.\n");
	RotateQ(&queue1);
	RotateQ(&queue2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);

	printf("\nAdding queue2 item = 3 to queue 1\n");
	AddQueue(&queue1, third);
	printf("Queue1: ");
	printQ(&queue1);
	
	printf("\nAdding queue1 item = 4 to queue 2\n");
	AddQueue(&queue2, fourth);
	printf("Queue2: ");
	printQ(&queue2);

	printf("\nAdding queue2 item = 2 to queue 1\n");
	AddQueue(&queue1, second);
	printf("Queue1: ");
	printQ(&queue1);
	
	printf("\nAdding queue1 item = 1 to queue 2\n");
	AddQueue(&queue2, first);
	printf("Queue2: ");
	printQ(&queue2);

	printf("\nRotating queues.\n");
	RotateQ(&queue1);
	RotateQ(&queue2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nDeleting head of queue2.\n");
	deleted2 = DelQueue(&queue2);
	printf("Queue2 deleted item is: %d\n", deleted2->data);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nAdding queue2 deleted item to queue1.\n");
	AddQueue(&queue1, deleted2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nDeleting head of queue2.\n");
	deleted2 = DelQueue(&queue2);
	printf("Queue2 deleted item is: %d\n", deleted2->data);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nAdding queue2 deleted item to queue1.\n");
	AddQueue(&queue1, deleted2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nDeleting head of queue2.\n");
	deleted2 = DelQueue(&queue2);
	printf("Queue1: ");
	printQ(&queue1);
	printf("Queue2: ");
	printQ(&queue2);
	
	printf("\nRotating queues.\n");
	RotateQ(&queue1);
	printf("Queue1: ");
	printQ(&queue1);
	RotateQ(&queue2);
	printf("Queue2: ");
	printQ(&queue2);
    
    return 0;
}
